DeDuplicationAgent properly handles destruction of memory

Dominik Sander vor 9 Jahren
Ursprung
Commit
ba91676d76
2 geänderte Dateien mit 24 neuen Zeilen und 2 gelöschten Zeilen
  1. 2 2
      app/models/agents/de_duplication_agent.rb
  2. 22 0
      spec/models/agents/de_duplication_agent_spec.rb

+ 2 - 2
app/models/agents/de_duplication_agent.rb

@@ -29,10 +29,10 @@ module Agents
29 29
     form_configurable :lookback
30 30
     form_configurable :expected_update_period_in_days
31 31
 
32
-    before_create :initialize_memory
32
+    after_initialize :initialize_memory
33 33
 
34 34
     def initialize_memory
35
-      memory['properties'] = []
35
+      memory['properties'] ||= []
36 36
     end
37 37
 
38 38
     def validate_options

+ 22 - 0
spec/models/agents/de_duplication_agent_spec.rb

@@ -40,6 +40,19 @@ describe Agents::DeDuplicationAgent do
40 40
     end
41 41
   end
42 42
 
43
+  describe '#initialize_memory' do
44
+    it 'sets properties to an empty array' do
45
+      expect(@checker.memory['properties']).to eq([])
46
+    end
47
+
48
+    it 'does not override an existing value' do
49
+      @checker.memory['properties'] = [1,2,3]
50
+      @checker.save
51
+      @checker.reload
52
+      expect(@checker.memory['properties']).to eq([1,2,3])
53
+    end
54
+  end
55
+
43 56
   describe "#working?" do
44 57
     before :each do
45 58
       # Need to create an event otherwise event_created_within? returns nil
@@ -123,5 +136,14 @@ describe Agents::DeDuplicationAgent do
123 136
       }.to change(Event, :count).by(1)
124 137
       expect(@checker.memory['properties'].last).to eq('3023526198')
125 138
     end
139
+
140
+    it "should still work after the memory was cleared" do
141
+      @checker.memory = {}
142
+      @checker.save
143
+      @checker.reload
144
+      expect {
145
+        @checker.receive([@event])
146
+      }.not_to raise_error
147
+    end
126 148
   end
127 149
 end